var app = getApp()
Page({

  /**
   * 页面的初始数据
   */
  data: {
    secArr: [],
    imgarr: [],
    img_arr: [],
    xztimes: "请选择时间",
    show: true,
    data_arr: [],
  },
  showhide() {
    this.setData({
      show: !this.data.show
    })
  },
  jcxm: function() {
    var that = this
    var p_id = that.data.erjid
    wx.navigateTo({
      url: '/hyb_yl/twosubpages/pages/xuanzexiangmu/xuanzexiangmu?p_id=' + p_id,
    })
  },
  shouzhenyiyuan() {
    wx.navigateTo({
      url: '/hyb_yl/twosubpages/pages/selhospital/selhospital',
    })
  },
  dingqiyisheng() {
    wx.navigateTo({
      url: '/hyb_yl/twosubpages/pages/dyisheng/dyisheng',
    })
  },
  nextjiuzhen() {
    wx.navigateTo({
      url: '/hyb_yl/twosubpages/pages/nextjiuzhen/nextjiuzhen',
    })
  },
  // 选择时间
  xztimes(e) {
    this.setData({
      xztimes: e.detail.value
    })
  },
  // 上传单据

  delimgs(e) {
    var dex = e.currentTarget.dataset.dex;
    var that = this;
    var arrs = that.data.imgarr;
    arrs.splice(dex, 1);
    that.setData({
      imgarr: arrs
    })
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function(options) {
    var that = this
    var ejdiv = options.ejdiv
    var xm = options.xm
    var bgc = wx.getStorageSync('color')
    wx.setNavigationBarColor({
      frontColor: "#ffffff",
      backgroundColor: bgc,
    })
    that.setData({
      ejdiv: ejdiv,
      xm: xm,
      bgc: bgc
    })
    if (options.jd_id !== undefined) {
      var jd_id = options.jd_id
      //查询详细信息
      app.util.request({
        'url': 'entry/wxapp/Selectjdinfo',
        data: {
          jd_id: jd_id,
        },
        success: function(res) {
          console.log(res)
          that.setData({
            detail: res.data.data,
            secArr: res.data.data.info,
            imgarr: res.data.data.org_pic,
            jcx_id: res.data.data.jcx_id,
          })
        }
      })
      that.setData({
        jd_id: jd_id
      })
    }
    if (options.erjid !== undefined) {
      var erjid = options.erjid
      app.util.request({
        'url': 'entry/wxapp/Alldiy',
        data: {
          id: erjid
        },
        success: function(res) {
          console.log(res)
          var data = res.data.data
          var obj = {}
          var secArr1 = JSON.parse(JSON.stringify(res.data.data))
          obj.secArr1 = []
          var tabArr = that.data.tabArr
          for (var i = 0; i < data.length; i++) {
            //console.log(data[i].displaytype)
            if (secArr1[i].displaytype == 2) {
              secArr1[i].picker = {}
              secArr1[i].picker.items = []
              secArr1[i].picker.displayorder = ""
              //console.log(secArr1[i].items)
              for (var j = 0, len = secArr1[i].items.length; j < len; j++) {
                console.log(1)
                secArr1[i].picker.items[j] = secArr1[i].items[j].title
              }
            }
          }
          var secArr = that.data.secArr
          //console.log(secArr1)
          secArr.push(obj)
          //console.log(secArr, data)
          that.setData({
            secArr: secArr1,
            data: res.data.data,
            currentEr: 0
          })
        },
      })
      that.setData({
        erjid: erjid
      })
    }
    app.util.request({
      'url': 'entry/wxapp/url',
      'cachetime': '0',
      success: function(res) {
        ///console.log(res)
        that.setData({
          url: res.data
        })
      },
    })
  },
  lineClick: function(e) {
    console.log(e)
    var that = this
    var secArr = that.data.secArr
    var idx = e.currentTarget.dataset.idx
    secArr[idx].description = e.detail.value
    that.setData({
      secArr: secArr
    })
  },
  choosePhoto: function(e) {
    var that = this
    var secArr = that.data.secArr
    console.log(secArr)
    var index = e.currentTarget.dataset.index
    var indexs = e.currentTarget.dataset.indexs
    var idx = e.currentTarget.dataset.idx
    wx.chooseImage({
      count: 9, // 默认9
      sizeType: ['original', 'compressed'], // 可以指定是原图还是压缩图，默认二者都有
      sourceType: ['album', 'camera'], // 可以指定来源是相册还是相机，默认二者都有
      success: function(res) {
        // 返回选定照片的本地文件路径列表，tempFilePath可以作为img标签的src属性显示图片
        secArr[idx].items = secArr[idx].items.concat(res.tempFilePaths)
        secArr[idx].description = secArr[idx].items
        if (secArr[idx].items.length >= 9) {
          secArr[idx].items.length = 9
        }
        console.log(secArr)
        that.setData({
          secArr: secArr
        })
      }
    })
  },
  delClick: function(e) {
    var that = this
    var secArr = that.data.secArr
    var index = e.currentTarget.dataset.index
    var indexs = e.currentTarget.dataset.indexs
    var idx = e.currentTarget.dataset.idx
    secArr[idx].items.splice(index, 1)
    that.setData({
      secArr: secArr
    })
  },
  //多行
  duohangClick: function(e) {
    //console.log(e)
    var that = this
    var secArr = that.data.secArr
    var indexs = e.currentTarget.dataset.indexs
    var idx = e.currentTarget.dataset.idx
    secArr[idx].description = e.detail.value
    that.setData({
      secArr: secArr
    })
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function() {
    this.getAlljcxm()
  },
  chooseCheck: function(e) {
    var that = this
    var index = e.currentTarget.dataset.index
    var idx = e.currentTarget.dataset.idx
    var secArr = that.data.secArr
    if (secArr[idx].items[index].checked) {
      secArr[idx].items[index].checked = false;
      secArr[idx].description += secArr[idx].items[index].title + ";"
    } else {
      secArr[idx].items[index].checked = true;
      secArr[idx].description = secArr[idx].description.replace(secArr[idx].items[index].title + ";", "")
    }
    that.setData({
      secArr: secArr
    })
  },
  //单选
  radioChange: function(e) {
    var that = this
    console.log(e)
    var secArr = that.data.secArr
    var idx = e.currentTarget.dataset.idx
    secArr[idx].description = e.detail.value
    console.log(secArr)
    this.setData({
      secArr: secArr
    })
  },
  //提交
  subClick: function(e) {

    var that = this
    var secArr = that.data.secArr
    console.log(secArr)

    if (that.data.ejdiv != 'undefined') {
      var erjid = that.data.ejdiv
    } else {
      var erjid = that.data.erjid
    }
    var xmname = that.data.txt
    var multsel = that.data.multsel
    if (that.data.jd_id) {
      var jd_id = that.data.jd_id
    }
    var data_arr = that.data.data_arr;

    this.addimgload();
    wx.showToast({
      title: '上传中，请稍后',
      icon: "loading"
    })

    var doctitle = that.data.doctitle
    for (var i = 0; i < secArr.length; i++) {
      delete secArr[i].uniacid
      delete secArr[i].activityid
      delete secArr[i].content
      // delete secArr[i].essential
      delete secArr[i].fieldstype
      if (secArr[i].displaytype != 3) {
        //delete secArr[i].highStandard
        delete secArr[i].lowStandard
      }
      if (secArr[i].displaytype != 0 && secArr[i].displaytype != 2 && secArr[i].displaytype != 1) {
        delete secArr[i].items
      }
    }
    var a = false;
    for (var i in secArr) {
      if (secArr[i].essential == '1' && secArr[i].description == '') {
        wx.showToast({
          title: secArr[i].title + "不能为空",
          icon: "none",
          duration: 1e3
        })
        //console.log('www')
        return false;
      } else {
        //  console.log('111')
        a = true
      }
    }
    if (a == true) {
      var values = JSON.stringify(secArr)
      console.log(values)
      console.log(secArr)
      var hosp = e.detail.value.hosp
      var xctime = e.detail.value.xctime
      var xztimes = that.data.xztimes
      if (that.data.jcx_id) {
        var jcx_id = that.data.jcx_id
      }
      if (e.detail.value.hosp == '') {
        wx.showToast({
          title: '请选择首诊医院',
          image: '../images/err.png'
        })
      } else if (xztimes == '') {
        wx.showToast({
          title: '请选择日期',
          image: '../images/err.png'
        })
      } else {
        setTimeout(function() {

          app.util.request({
            'url': 'entry/wxapp/Savedivjd',
            data: {
              values: values,
              fuleiid: erjid,
              jd_id: jd_id,
              openid: wx.getStorageSync('openid'),
              org_pic: data_arr,
              timearr: xztimes,
              hosp: hosp,
              xctime: xctime,
              xmname: xmname,
              jcx_id: jcx_id,
              multsel: multsel,
              erjid: that.data.ejdiv
            },
            success: function(res) {
              console.log(res)
              wx.showToast({
                title: '保存成功',
                icon: 'success',
                duration: 2000,
                success: function() {
                  setTimeout(function() {
                    //要延时执行的代码
                    wx.navigateBack({
                      delta: 1
                    })
                  }, 2000) //延迟时间
                }
              })
            }
          })
        }, 3500)
      }
    }

  },

  //picker
  bloodClick: function(e) {
    console.log('picker发送选择改变，携带值为', e.detail.value)
    var that = this
    var secArr = that.data.secArr
    var idx = e.currentTarget.dataset.idx
    secArr[idx].picker.displayorder = e.detail.value
    //secArr.description = secArr.arr[e.detail.value]
    for (var i = 0; i < secArr.length; i++) {
      if (secArr[i].displaytype == 2) {
        secArr[i].displayorder = secArr[i].items[e.detail.value].title;
      }
    }
    console.log(secArr)
    this.setData({
      secArr: secArr
    })
  },
  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function() {
    var that = this
    let pages = getCurrentPages();
    let currPage = pages[pages.length - 1];
    if (currPage.data.yiyuan) {
      that.setData({
        yiyuan: currPage.data.yiyuan,
      })
      if (currPage.data.xctime) {
        xctime: currPage.data.xctime
      }
      if (currPage.data.z_name) {
        z_name: currPage.data.z_name
      }
    }
    if (currPage.data.z_name) {
      var secArr = that.data.secArr
      for (var i = 0; i < secArr.length; i++) {
        if (secArr[i].displaytype == 6) {
          secArr[i].displayorder = currPage.data.z_name;
        }
      }
      console.log(secArr)
      that.setData({
        secArr: secArr
      })
    }
    if (currPage.data.txt) {
      that.setData({ //将携带的参数赋值
        txt: currPage.data.txt
      });
    }
    if (currPage.data.xm_id) {
      that.setData({ //将携带的参数赋值
        xm_id: currPage.data.xm_id
      });

    }
    if (currPage.data.jc_jgtype) {
      that.setData({ //将携带的参数赋值
        multsel: currPage.data.jc_jgtype
      });
    }
    if (currPage.data.jcx_id) {
      that.setData({ //将携带的参数赋值
        jcx_id: currPage.data.jcx_id
      });
    }
    //console.log(currPage.data.jcx_id)
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function() {

  },
  addimgload() {
    var that = this
    var uniacid = app.siteInfo.uniacid
    var data_arr = that.data.data_arr;
    //console.log(uniacid, that.data.url)
    for (var i = 0; i < this.data.imgarr.length; i++) {
      wx.uploadFile({
        url: that.data.url + 'app/index.php?i=' + uniacid + '&c=entry&a=wxapp&do=Upload&m=hyb_yl',
        filePath: that.data.imgarr[i],
        name: 'upfile',
        formData: [],
        success: function(res) {
          console.log(res)
          data_arr.push(res.data)
          that.setData({
            data_arr: data_arr
          })
        }
      })
    }
    this.setData({
      formdata: ''
    })
  },
  upimg: function() {
    var that = this;
    console.log(this.data.imgarr)
    if (this.data.imgarr.length < 3) {
      wx.chooseImage({
        count: 3,
        sizeType: ['original', 'compressed'],
        success: function(res) {
          console.log(res)
          if (res.tempFilePaths.length == 3) {
            that.setData({
              hide: true
            })
          }
          that.setData({
            imgarr: that.data.imgarr.concat(res.tempFilePaths)
          })
        }
      })
    } else {
      wx.showToast({
        title: '最多上传三张图片',
        icon: 'loading',
        duration: 3000
      });
    }
  },
  fanhui: function() {
    wx.navigateBack({
      delta: 1
    })
  },
  //查询当前栏目下的所有检查项目
  getAlljcxm: function() {
    var that = this
    var p_id = that.data.erjid
    console.log(p_id)
    var xm = that.data.xm
    console.log(xm)
    app.util.request({
      'url': 'entry/wxapp/Alljcxm',
      data: {
        p_id: p_id
      },
      success: function(res) {
        console.log(res)
        that.setData({
          jcxm: res.data.data,
          xm: xm
        })
      }
    })


  },
  getJcjg: function(e) {
    var that = this
    var xm_id = that.data.xm_id
    var multsel = that.data.multsel
    // var erjid = that.data.erjid
    if (that.data.multsel !== undefined) {

      var multsel = that.data.multsel
    } else {

      var multsel = e.currentTarget.dataset.multsel
      var multtypes = 1
    }
    if (that.data.erjid !== undefined) {

      var erjid = that.data.erjid
    } else {

      var erjid = e.currentTarget.dataset.erjid
    }
    var jcx_id = e.currentTarget.dataset.jcx_id
    if (that.data.jcx_id) {
      wx.navigateTo({
        url: '/hyb_yl/twosubpages/pages/jianchajieguo/jianchajieguo?xm_id=' + xm_id + "&multsel=" + multsel + '&erjid=' + erjid + '&jcx_id=' + that.data.jcx_id + '&multtypes=' + multtypes,
      })
    } else {
      wx.navigateTo({
        url: '/hyb_yl/twosubpages/pages/jianchajieguo/jianchajieguo?xm_id=' + xm_id + "&multsel=" + multsel + '&erjid=' + erjid,
      })
    }

  }
})